<template>
<div>
  <div class="t1">
    <span>个人评论</span>
  </div>
<!--  <div style="padding: 10px 0">
    <el-input style="width: 200px" placeholder="请输入标题" suffix-icon="el-icon-search" v-model="title"></el-input>
    <el-input style="width: 200px" placeholder="请输入作者" suffix-icon="el-icon-user" class="ml-5" v-model="author"></el-input>
    <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
    <el-button class="ml-5" type="warning" @click="reset">重置</el-button>
  </div>-->

  <div style="margin: 10px 0">
<!--    <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-circle-plus-outline"></i></el-button>-->
    <el-button type="danger" @click="deleteBatch" plain>批量删除 <i class="el-icon-remove-outline"></i></el-button>
  </div>

  <el-table :data="tableData" border stripe header-cell-class-name="headerBg"
            @selection-change="handleSelectionChange">
    <el-table-column type="selection" width="55"></el-table-column>
    <el-table-column prop="id" label="ID" width="80"></el-table-column>
    <el-table-column prop="physician.nickname" label="医生"></el-table-column>
    <el-table-column prop="user.nickname" label="用户" width="120" v-if="false"></el-table-column>
    <el-table-column prop="content" label="评论"></el-table-column>
    <el-table-column prop="score" label="评分"></el-table-column>
    <el-table-column label="操作" width="180px">
      <template slot-scope="scope">
        <el-button type="primary" @click="handleEdit(scope.row)" plain>编辑 <i class="el-icon-edit"></i></el-button>
        <el-button type="danger" @click="del(scope.row.id)" plain>删除 <i class="el-icon-remove-outline"></i></el-button>
      </template>
    </el-table-column>
  </el-table>
  <div style="padding: 10px 0">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[2, 5, 10, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>

  <el-dialog title="评论信息" :visible.sync="dialogFormVisible" width="30%">
    <el-form label-width="80px" size="small">
      <el-form-item label="医生">
        <el-select clearable v-model="form.phyId" placeholder="请选择">
          <el-option v-for="item in physicians" :key="item.id" :label="item.nickname" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
        <input v-model="form.userId" v-if="false">
<!--        <el-select clearable v-model="form.userId" placeholder="请选择">
          <el-option v-for="item in users" :key="item.id" :label="item.nickname" :value="item.id"></el-option>
        </el-select>-->
      <el-form-item label="评论">
        <el-input type="textarea" v-model="form.content" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="评分">
        <el-select v-model="form.score" placeholder="请选择评分">
          <el-option label="10" value="10"></el-option>
          <el-option label="9" value="9"></el-option>
          <el-option label="8" value="8"></el-option>
          <el-option label="7" value="7"></el-option>
          <el-option label="6" value="6"></el-option>
          <el-option label="5" value="5"></el-option>
          <el-option label="4" value="4"></el-option>
          <el-option label="3" value="3"></el-option>
          <el-option label="2" value="2"></el-option>
          <el-option label="1" value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</div>
</template>

<script>
export default {
  name: "Comment",
  data() {
    return {
      tableData: {},
      total: 0,
      pageNum: 1,
      pageSize: 10,
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      title: "",
      author: "",
      physicians: [],
      users: [],
      dialogFormVisible: false,
      form: {},
      multipleSelection: [],
      headerBg: 'headerBg'
    }
  },
  created() {
    //请求分页查询数据
    this.load()
  },
  methods: {
    load() {
      this.request.get("/appraise/userAppraise", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          id: this.user.id
          //title: this.title,
          //author: this.author
        }
      }).then(res => {
        console.log(res)
        this.tableData = res.data
        this.total = res.total
      })
    },
    save() {
      this.form.userId = this.user.id
      this.request.post("/appraise/edit", this.form).then(res => {
        if (res) {
          this.$message.success("保存成功")
          this.dialogFormVisible = false
          this.load()
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAdd() {
      this.request.get("/appraise/findPhy").then(res => {
        this.physicians = res
      })
      this.request.get("/appraise/findUser").then(res => {
        this.users = res
      })
      this.dialogFormVisible = true
      this.form = {}
    },
    handleEdit(row) {
      this.request.get("/appraise/findPhy").then(res => {
        this.physicians = res
      })
      this.request.get("/appraise/findUser").then(res => {
        this.users = res
      })
      this.form = Object.assign({}, row)
      console.log(this.form)
      this.dialogFormVisible = true
    },
    del(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/appraise/del?id=" + id).then(res => {
          if (res) {
            this.$message.success("删除成功")
            this.load()
          } else {
            this.$message.error("删除失败")
          }
        })
      });
    },
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val
    },
    deleteBatch() {
      let ids = this.multipleSelection.map(v => v.id) //将对象数组变成纯id数组 [{},{},{}] => [1,2,3]
      this.$confirm('此操作将永久批量删除选中数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.delete("/appraise/delBatch", {data: ids}).then(res => {
          if (res) {
            this.$message.success("批量删除成功")
            this.load()
          } else {
            this.$message.error("批量删除失败")
          }
        })
      });
    },
    reset() {
      this.title = ""
      this.author = ""
      this.load()
    },
    handleSizeChange(pageSize) {
      console.log(pageSize)
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      console.log(pageNum)
      this.pageNum = pageNum
      this.load()
    },
    exp() {
      window.open("http://localhost:8082/appraise/export")
    },
    handleExcelImportSuccess() {
      this.$message.success("导入成功")
      this.load()
    },
  }
}
</script>

<style scoped>
.t1{
  font-size: 20px;
  color: #409EFF;
  margin-top: 10px;
  margin-bottom: 10px;
  font-style:italic;
  text-decoration: underline;
}
</style>
